﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Daq.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Daq.ViewModels.DeviceSettingsViewModels
{
    public partial class MeasurementViewModel : ObservableObject
    {
        public MeasurementViewModel(SettingsService settings)
        {
            Settings = settings;
            //Settingses = new ObservableCollection<SystemSettings>() { Settings.SystemSettings.Instance };
        }

        [ObservableProperty]
        private SettingsService settings;

        [ObservableProperty]
        private string? name = "测量设置";

        [ObservableProperty]
        private string? capacityRemain;

        [RelayCommand]
        private void GoToPreviousStep()
        {
            var vm = App.Current.Services.GetService<DeviceSettingsViewModel>();
            vm?.SelectStep("通道设置");
        }

        [RelayCommand]
        private void GoToNextStep()
        {
            var vm = App.Current.Services.GetService<DeviceSettingsViewModel>();
            vm?.SelectStep("预处理设置");
        }

        [RelayCommand]
        private async Task SetDirectory()
        {
            OpenFolderDialog dialog = new()
            {
                Title = "选择文件夹",
                InitialDirectory = Settings.SystemSettings.Instance.DataDirectory
            };
            if (dialog.ShowDialog() == true)
            {
                Settings.SystemSettings.Instance.DataDirectory = dialog.FolderName;
                await GetCapacityRemain();
            }
        }

        private static async Task<double> GetDiskFreeSpace(string directory)
        {
            return await Task.Run(() =>
            {
                try
                {
                    // 获取指定目录所在的驱动器
                    DriveInfo drive = new DriveInfo(Path.GetPathRoot(directory));

                    // 检查驱动器是否准备好
                    if (drive.IsReady)
                    {
                        // 计算以 G 为单位的剩余空间
                        return (double)drive.AvailableFreeSpace / (1024 * 1024 * 1024);
                    }
                    else
                    {
                        Debug.WriteLine("驱动器未准备好。");
                        return -1;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"发生错误: {ex.Message}");
                    return -1;
                }
            });
        }


        [ObservableProperty]
        private string? capacitySmallWarningMessage;

        [RelayCommand]
        private async Task GetCapacityRemain()
        {
            var freeSpace = await GetDiskFreeSpace(Settings.SystemSettings.Instance.DataDirectory);
            CapacityRemain = freeSpace < 0 ? "--" : $"{freeSpace:F2} GB";
            if(freeSpace < 1)
            {
                CapacitySmallWarningMessage = "⚠ 可用容量过低，请重新选择保存位置或删除文件以释放空间！";
            }
            else
            {
                CapacitySmallWarningMessage = string.Empty;
            }
        }
    }
}
